package com.facishare.open.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.facishare.open.ajax.code.AjaxCode;
import com.facishare.open.ajax.result.AjaxResult;
import com.facishare.open.entity.Blog;
import com.facishare.open.entity.BlogVO;
import com.facishare.open.entity.User;
import com.facishare.open.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * Created by zenglb on 2016/7/19.
 */

@RestController
@Slf4j
@RequestMapping("/blog")
public class BlogController extends BaseController {
    @Resource
    private BlogService blogService;

    @RequestMapping("/getAllBlogs")
    public AjaxResult getAllBlogs() {

        log.info("com.facishare.open.controller.BlogController.getAllBlogs()");
//        String data = "";
        BlogVO blogVO = new BlogVO();
        try{
            List<Blog> bList = blogService.getAllBlogs();

            if(bList != null && bList.size()>0){
                blogVO.setBlogs(bList);
                blogVO.setSize(bList.size());
            }



//            if(bList.size()>0) {
//                for(Blog blog:bList) {
//                    data+=blog+",";
//                }
//                data = data.substring(0,data.length()-1);
//            }
        } catch (Exception e) {
            return new AjaxResult(AjaxCode.BIZ_EXCEPTION,e.toString());
        }

        return new AjaxResult(blogVO);
    }

    @RequestMapping("/getBlogById")
    public AjaxResult getBlogById(long id) {

        log.info("com.facishare.open.controller.BlogController.getBlogById()");
        Blog blog = null;
        try {
            blog = blogService.getBlogById(id);
        }catch(Exception e) {
            return new AjaxResult(AjaxCode.BIZ_EXCEPTION,e.toString());
        }
        return new AjaxResult(blog);
    }

    @RequestMapping("/insertAndGetId")
    public AjaxResult insertAndGetId(Blog blog) {

        log.info("com.facishare.open.controller.BlogController.insertAndGetId()");
        try {
            blogService.insertAndGetId(blog);
        }catch (Exception e) {
            return new AjaxResult(AjaxCode.BIZ_EXCEPTION,e.toString());
        }
        return new AjaxResult(blog.getId());
    }

    @RequestMapping("/getBlogsByWhere")
    public AjaxResult getBlogsByWhere(Blog blog) {

        log.info("com.facishare.open.controller.BlogController.getBlogsByWhere()");

        try {
            List<Blog> bList = blogService.getBlogsByWhere(blog);
            log.info(bList.get(0).toString());
        }catch (Exception e) {
            return new AjaxResult(AjaxCode.BIZ_EXCEPTION,e.toString());
        }
        return SUCCESS;
    }

}
